using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using WorldSource.Entities;
using WorldSource.GUI;

namespace WorldSource
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static Game game;
        List<GameObject> gameObjects = new List<GameObject>();
        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        public static Player player;
        public static InputHandler inputHandler;
        public static GUIHandler guiHandler;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            game = this;
            IsMouseVisible = true;
            base.Initialize();
        }


        protected override void LoadContent()
        {
            inputHandler = new InputHandler();
            guiHandler = new GUIHandler();
            spriteBatch = new SpriteBatch(GraphicsDevice);
            player = new Player(this, Content.Load<Texture2D>("alienPlayer"));
            player.Position = new Vector2(25, 25);
            gameObjects.Add(player);

            Zombie zombie = new Zombie(this, Content.Load<Texture2D>("zombie"));
            zombie.Position = new Vector2(0, 25);
            gameObjects.Add(zombie);
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            inputHandler.keyBoardState = Keyboard.GetState();
            inputHandler.mouseState = Mouse.GetState();
            inputHandler.Update(gameTime);
            guiHandler.Update();
            foreach (GameObject gameObject in gameObjects)
            {
                gameObject.Update(gameTime);
            }
            inputHandler.prevMouseState = inputHandler.mouseState;
            inputHandler.prevKeyBoardState = inputHandler.keyBoardState;
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            //Static GUI
            spriteBatch.Begin();
            guiHandler.Draw();
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, null, Camera.mainCamera.getTransformation());
            foreach (GameObject gameObject in gameObjects)
            {
                gameObject.Draw(gameTime);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
